﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FloorDesigner;
using OpenRPOSBackOffice.ServiceReferenceOpenRPOS;
namespace OpenRPOSBackOffice
{
    public partial class LayoutViewer : UserControl
    {
        public int _lastLayout=0;
        public LayoutViewer()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutContainer.Children.Clear();
            Globals.showWaitCursor(true);
            //populate layouts dropdown
            fillLayouts();
            Globals.showWaitCursor(false);
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            //
        }
        
        private async void fillLayouts()
        {
            ddlLayout.Items.Clear();
            await Globals.db.GetTableLayoutObjects("1=1 order by Name"); //fetch records from database

            foreach (TableLayout tableLayout in Globals.db.TableLayoutRecords)
            {
                ddlLayout.Items.Add(new ComboBoxItem() { Tag = tableLayout.col_ID, Content = tableLayout.col_Name.Trim() });
            }

            if (ddlLayout.Items.Count > 0)
            {
                if (_lastLayout > 0) ddlLayout.SelectedValue = _lastLayout;
                else ddlLayout.SelectedIndex = 0;
            }
        }

        private async void ddlLayout_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Globals.showWaitCursor(true);
            if (((ComboBox)sender).SelectedItem != null)
            {
                await Globals.db.GetTableLayoutObjects("ID=" + Convert.ToInt32(((ComboBox)sender).SelectedValue));
                if (Globals.db.TableLayoutRecords.Count > 0)
                {
                    lblHeader.Content = Globals.db.TableLayoutRecords[0].col_Name + " (Width : " + Globals.db.TableLayoutRecords[0].col_Length + ", Height : " + Globals.db.TableLayoutRecords[0].col_Width + ")";
                    LayoutContainer.Children.Clear();
                    
                    LayoutContainer.Width = Globals.db.TableLayoutRecords[0].col_Length;
                    LayoutContainer.Height = Globals.db.TableLayoutRecords[0].col_Width;
                    
                    LayoutContainer.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    LayoutContainer.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    CreateTable(Globals.db.TableLayoutRecords[0].col_ID);                    
                }
            }
            Globals.showWaitCursor(false);
        }

        private async void CreateTable(int layoutID)
        {
            await Globals.db.GetTableSpecificationObjects("TableLayoutID=" + layoutID + " order by TableNumber");

            if (Globals.db.TableSpecificationRecords.Count > 0)
            {
                foreach (TableSpecification oTableSpecification in Globals.db.TableSpecificationRecords)
                {
                    CreateCanvas(oTableSpecification);
                }
            }
        }

        private void CreateCanvas(TableSpecification oTableSpecification)
        {
            try
            {
                string canvasName=Convert.ToString(oTableSpecification.col_TableNumber);
                string canvasID=Convert.ToString(oTableSpecification.col_ID);
                Globals.TableType tableShapeType=(oTableSpecification.col_Rectangle ? Globals.TableType.Ractangle : Globals.TableType.Ellipse);
                double width = oTableSpecification.col_Length;
                double height=oTableSpecification.col_Width;
                double top=oTableSpecification.col_CenterY;
                double left=oTableSpecification.col_CenterX;
                bool tableType=(tableShapeType == Globals.TableType.Ractangle ? true : false);

                TableControl controlCanvas = new TableControl();
                controlCanvas.Name = canvasID;
                controlCanvas.Tag = canvasID;
                controlCanvas.IsRactangle = tableType;

                controlCanvas.Width = width;
                controlCanvas.Height = height;
                
                controlCanvas.Foreground = new SolidColorBrush(Colors.Red);
                controlCanvas.BorderBrush = new SolidColorBrush(Colors.Red);
                
                controlCanvas.Content = canvasName;
                
                controlCanvas.AllowedDropContainers = new Panel[] { LayoutContainer };
                controlCanvas.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                controlCanvas.AllowCopy = false;

                controlCanvas.BorderThickness = new Thickness(2.0);
                controlCanvas.Padding = new Thickness(2.0);
                controlCanvas.SetValue(Canvas.LeftProperty, left);
                controlCanvas.SetValue(Canvas.TopProperty, top);

                RadialGradientBrush fiveColorRGB = new RadialGradientBrush();
                GradientStop blueGS = new GradientStop();
                GradientStop orangeGS = new GradientStop();

                Color tableColor = Colors.Blue;
                if (oTableSpecification.col_Occupied == true)
                {
                    tableColor = Colors.Green;
                }
                switch (tableShapeType)
                {
                    case Globals.TableType.Ellipse:
                        controlCanvas.Style = this.Resources["EllipseStyle"] as Style;
                        blueGS.Color = tableColor;
                        orangeGS.Color = Colors.Transparent;

                        blueGS.Offset = 1;
                        orangeGS.Offset = 1;
                        break;

                    case Globals.TableType.Ractangle:
                        controlCanvas.Style = this.Resources["RectangleStyle"] as Style;
                        blueGS.Color = tableColor;
                        orangeGS.Color = tableColor;
                        blueGS.Offset = 1;
                        orangeGS.Offset = 1;
                        break;
                }

                fiveColorRGB.GradientStops.Add(blueGS);
                fiveColorRGB.GradientStops.Add(orangeGS);
                
                controlCanvas.Background = fiveColorRGB;
                controlCanvas.IsNew = false;
                controlCanvas.DragImageVisibility = false;
                var myElement = (LayoutContainer.Children.SingleOrDefault<UIElement>(label => label.GetValue(FrameworkElement.NameProperty).ToString() == Convert.ToString(controlCanvas.Tag)));

                if (myElement == null)
                {
                    LayoutContainer.Children.Add(controlCanvas);
                }
            }
            catch (Exception ex)
            {
                //log exception 
            }

        }
    }
}
